package fr.afcepf.ai89.esharing.managedbean;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.richfaces.model.CalendarDataModel;
import org.richfaces.model.CalendarDataModelItem;

import fr.afcepf.ai89.esharing.entity.Emprunt;
import fr.afcepf.ai89.esharing.entity.InvisibiliteProprietaire;
import fr.afcepf.ai89.esharing.entity.Proprietaire;
import fr.afcepf.ai89.esharing.metier.BusinessEmpruntAem;
import fr.afcepf.ai89.esharing.metier.BusinessGestionCompte;
import fr.afcepf.ai89.esharing.metier.BusinessPretAem;
import fr.afcepf.ai89.esharing.metier.IBusinessEmpruntAem;
import fr.afcepf.ai89.esharing.metier.IBusinessGestionCompte;
import fr.afcepf.ai89.esharing.metier.IBusinessPretAem;

public class MonAgendaManagedBean {

	ConnexionManagedBean mbConnexion;

	private CalendarDataModel model = new CalendarModel();
	private Date lastDate = new Date();
	private Date firstDate = new Date();
	private List<Emprunt> empruntsProgrammes;
	private List<Emprunt> empruntsEnCours;
	private List<Emprunt> empruntsTermines;
	private List<Emprunt> pretsProgrammes;
	private List<Emprunt> pretsEnCours;
	private List<Emprunt> pretsTermines;
	private List<InvisibiliteProprietaire> conges;
	private IBusinessEmpruntAem busEmpAem = new BusinessEmpruntAem();
	private IBusinessPretAem busPretAem = new BusinessPretAem();
	private IBusinessGestionCompte busGesCo = new BusinessGestionCompte();

	public ConnexionManagedBean getMbConnexion() {
		return mbConnexion;
	}

	public void setMbConnexion(ConnexionManagedBean mbConnexion) {
		this.mbConnexion = mbConnexion;
	}

	public MonAgendaManagedBean() {

		// Proprietaire prop = mbConnexion.getPropConnecte().getProp();

		// //System.out.println("***************" + prop.getNomUtilisateur()
		// + "****************");
		//System.out.println("***************" + "DOES THE DAMN THING WORK??"
//				+ "****************");
//		//System.out.println("***************" + "THEN DON'T FUCK WITH IT!!!"
//				+ "****************");

	}

	public CalendarDataModel getModel() {
		return model;
	}

	public void setModel(CalendarDataModel model) {
		this.model = model;
	}

	public Date getLastDate() {
		lastDate = new Date(292278994);
		for (Emprunt emprunt : empruntsProgrammes) {
			if (lastDate.before(emprunt.getDateFin())) {
				lastDate = emprunt.getDateFin();
			}
		}
		for (Emprunt emprunt : empruntsEnCours) {
			if (lastDate.before(emprunt.getDateFin())) {
				lastDate = emprunt.getDateFin();
			}
		}
		for (Emprunt emprunt : empruntsTermines) {
			if (lastDate.before(emprunt.getDateFin())) {
				lastDate = emprunt.getDateFin();
			}
		}
		for (Emprunt pret : pretsProgrammes) {
			if (lastDate.before(pret.getDateFin())) {
				lastDate = pret.getDateFin();
			}
		}
		for (Emprunt pret : pretsEnCours) {
			if (lastDate.before(pret.getDateFin())) {
				lastDate = pret.getDateFin();
			}
		}
		for (Emprunt pret : pretsTermines) {
			if (lastDate.before(pret.getDateFin())) {
				lastDate = pret.getDateFin();
			}
		}
		for (InvisibiliteProprietaire conge : conges) {
			if (lastDate.before(conge.getDateFinInvisibiliteProp())) {
				lastDate = conge.getDateFinInvisibiliteProp();
			}
		}
		return lastDate;
	}

	public void setLastDate(Date lastDate) {
		this.lastDate = lastDate;
	}

	public class CalendarModel implements CalendarDataModel {

		@Override
		public CalendarDataModelItem[] getData(Date[] dates) {
			CalendarDataModelItem[] modelItems = new CalendarModelItem[dates.length];
			if (!empruntsProgrammes.isEmpty()) {
				for (Emprunt emprunt : empruntsProgrammes) {
					Date dateDebutEmpruntsProgrammes = new Date(emprunt
							.getDateDebut().getTime());
					Date dateFinEmpruntsProgrammes = new Date(emprunt
							.getDateFin().getTime());
					for (int i = 0; i < dates.length; i++) {
						Date dateCourante = new Date(dates[i].getTime());
						CalendarModelItem cmi = new CalendarModelItem();
						if (dateCourante.equals(dateDebutEmpruntsProgrammes)) {
							cmi.setEnabled(false);
							cmi.setStyleClass("red");
						}
						if (dateCourante.equals(dateFinEmpruntsProgrammes)) {
							cmi.setEnabled(false);
							cmi.setStyleClass("red");
						}
						if (dateCourante.after(dateDebutEmpruntsProgrammes)
								&& dateCourante
										.before(dateFinEmpruntsProgrammes)) {
							cmi.setEnabled(false);
							cmi.setStyleClass("red");
						}
						modelItems[i] = cmi;
					}
				}
			}
			if (!empruntsEnCours.isEmpty()) {
				for (Emprunt emprunt : empruntsEnCours) {
					Date dateDebutEmpruntsEnCours = new Date(emprunt
							.getDateDebut().getTime());
					Date dateFinEmpruntsEnCours = new Date(emprunt.getDateFin()
							.getTime());
					for (int i = 0; i < dates.length; i++) {
						Date dateCourante = new Date(dates[i].getTime());
						CalendarModelItem cmi = (CalendarModelItem) modelItems[i];
						if (cmi == null) {
							cmi = new CalendarModelItem();
						}
						if (dateCourante.equals(dateDebutEmpruntsEnCours)) {
							cmi.setEnabled(false);
							cmi.setStyleClass("red");
						}
						if (dateCourante.equals(dateFinEmpruntsEnCours)) {
							cmi.setEnabled(false);
							cmi.setStyleClass("red");
						}
						if (dateCourante.after(dateDebutEmpruntsEnCours)
								&& dateCourante.before(dateFinEmpruntsEnCours)) {
							cmi.setEnabled(false);
							cmi.setStyleClass("red");
						}
						modelItems[i] = cmi;
					}
				}
			}
			if (!empruntsTermines.isEmpty()) {
				for (Emprunt emprunt : empruntsTermines) {
					Date dateDebutEmpruntsEnCours = new Date(emprunt
							.getDateDebut().getTime());
					Date dateFinEmpruntsEnCours = new Date(emprunt.getDateFin()
							.getTime());
					for (int i = 0; i < dates.length; i++) {
						Date dateCourante = new Date(dates[i].getTime());
						CalendarModelItem cmi = (CalendarModelItem) modelItems[i];
						if (cmi == null) {
							cmi = new CalendarModelItem();
						}
						if (dateCourante.equals(dateDebutEmpruntsEnCours)) {
							cmi.setEnabled(false);
							cmi.setStyleClass("red");
						}
						if (dateCourante.equals(dateFinEmpruntsEnCours)) {
							cmi.setEnabled(false);
							cmi.setStyleClass("red");
						}
						if (dateCourante.after(dateDebutEmpruntsEnCours)
								&& dateCourante.before(dateFinEmpruntsEnCours)) {
							cmi.setEnabled(false);
							cmi.setStyleClass("red");
						}
						modelItems[i] = cmi;
					}
				}
			}
			if (!pretsProgrammes.isEmpty()) {
				for (Emprunt pret : pretsProgrammes) {
					Date dateDebutEmpruntsEnCours = new Date(pret
							.getDateDebut().getTime());
					Date dateFinEmpruntsEnCours = new Date(pret.getDateFin()
							.getTime());
					for (int i = 0; i < dates.length; i++) {
						Date dateCourante = new Date(dates[i].getTime());
						CalendarModelItem cmi = (CalendarModelItem) modelItems[i];
						if (cmi == null) {
							cmi = new CalendarModelItem();
						}
						if (dateCourante.equals(dateDebutEmpruntsEnCours)) {
							cmi.setEnabled(false);
							cmi.setStyleClass("green");
						}
						if (dateCourante.equals(dateFinEmpruntsEnCours)) {
							cmi.setEnabled(false);
							cmi.setStyleClass("green");
						}
						if (dateCourante.after(dateDebutEmpruntsEnCours)
								&& dateCourante.before(dateFinEmpruntsEnCours)) {
							cmi.setEnabled(false);
							cmi.setStyleClass("green");
						}
						modelItems[i] = cmi;
					}
				}
			}
			if (!pretsEnCours.isEmpty()) {
				for (Emprunt pret : pretsEnCours) {
					Date dateDebutEmpruntsEnCours = new Date(pret
							.getDateDebut().getTime());
					Date dateFinEmpruntsEnCours = new Date(pret.getDateFin()
							.getTime());
					for (int i = 0; i < dates.length; i++) {
						Date dateCourante = new Date(dates[i].getTime());
						CalendarModelItem cmi = (CalendarModelItem) modelItems[i];
						if (cmi == null) {
							cmi = new CalendarModelItem();
						}
						if (dateCourante.equals(dateDebutEmpruntsEnCours)) {
							cmi.setEnabled(false);
							cmi.setStyleClass("green");
						}
						if (dateCourante.equals(dateFinEmpruntsEnCours)) {
							cmi.setEnabled(false);
							cmi.setStyleClass("green");
						}
						if (dateCourante.after(dateDebutEmpruntsEnCours)
								&& dateCourante.before(dateFinEmpruntsEnCours)) {
							cmi.setEnabled(false);
							cmi.setStyleClass("green");
						}
						modelItems[i] = cmi;
					}
				}
			}
			if (!pretsTermines.isEmpty()) {
				for (Emprunt pret : pretsTermines) {
					Date dateDebutEmpruntsEnCours = new Date(pret
							.getDateDebut().getTime());
					Date dateFinEmpruntsEnCours = new Date(pret.getDateFin()
							.getTime());
					for (int i = 0; i < dates.length; i++) {
						Date dateCourante = new Date(dates[i].getTime());
						CalendarModelItem cmi = (CalendarModelItem) modelItems[i];
						if (cmi == null) {
							cmi = new CalendarModelItem();
						}
						if (dateCourante.equals(dateDebutEmpruntsEnCours)) {
							cmi.setEnabled(false);
							cmi.setStyleClass("green");
						}
						if (dateCourante.equals(dateFinEmpruntsEnCours)) {
							cmi.setEnabled(false);
							cmi.setStyleClass("green");
						}
						if (dateCourante.after(dateDebutEmpruntsEnCours)
								&& dateCourante.before(dateFinEmpruntsEnCours)) {
							cmi.setEnabled(false);
							cmi.setStyleClass("green");
						}
						modelItems[i] = cmi;
					}
				}
			}
			if (!conges.isEmpty()) {
				//System.out.println(lastDate);
				for (InvisibiliteProprietaire conge : conges) {
					Date dateDebutConges = new Date(conge
							.getDateDebutInvisibiliteProp().getTime());
					Date dateFinConges = new Date(conge
							.getDateFinInvisibiliteProp().getTime());
					for (int i = 0; i < dates.length; i++) {
						Date dateCourante = new Date(dates[i].getTime());
						CalendarModelItem cmi = (CalendarModelItem) modelItems[i];
						if (cmi == null) {
							cmi = new CalendarModelItem();
						}
						if (dateCourante.equals(dateDebutConges)) {
							cmi.setEnabled(false);
							cmi.setStyleClass("yellow");
						}
						if (dateCourante.equals(dateFinConges)) {
							cmi.setEnabled(false);
							cmi.setStyleClass("yellow");
						}
						if (dateCourante.after(dateDebutConges)
								&& dateCourante.before(dateFinConges)) {
							cmi.setEnabled(false);
							cmi.setStyleClass("yellow");
						}
						modelItems[i] = cmi;
					}
				}
			}
			return modelItems;
		}

		@Override
		public Object getToolTip(Date dates) {
			// TODO Auto-generated method stub
			return null;
		}

		public class CalendarModelItem implements CalendarDataModelItem {

			private boolean enabled = true;
			private String styleClass = "";

			public void setEnabled(boolean enabled) {
				this.enabled = enabled;
			}

			public void setStyleClass(String styleClass) {
				this.styleClass = this.styleClass + styleClass;
				if (this.styleClass.equals("redred")) {
					this.styleClass = "red";
				}
				if (this.styleClass.equals("redyellow")) {
					this.styleClass = "yellow";
				}
				if (this.styleClass.equals("redgreenyellow")) {
					this.styleClass = "yellow";
				}
			}

			@Override
			public Object getData() {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public int getDay() {
				// TODO Auto-generated method stub
				return 0;
			}

			@Override
			public String getStyleClass() {
				return styleClass;
			}

			@Override
			public Object getToolTip() {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public boolean hasToolTip() {
				// TODO Auto-generated method stub
				return false;
			}

			@Override
			public boolean isEnabled() {
				return enabled;
			}

		}
	}

	public Date getFirstDate() {
		empruntsProgrammes = mbConnexion.getPropConnecte()
				.getEmpruntsProgrammes();
		empruntsEnCours = mbConnexion.getPropConnecte().getEmpruntsEnCours();
		empruntsTermines = mbConnexion.getPropConnecte().getEmpruntsTermines();
		pretsProgrammes = mbConnexion.getPropConnecte().getPretsProgrammes();
		pretsEnCours = mbConnexion.getPropConnecte().getPretsEnCours();
		pretsTermines = mbConnexion.getPropConnecte().getPretsTermines();
		conges = busGesCo.getConges(mbConnexion.getPropConnecte().getProp());
		firstDate = new Date(1970, 1, 1);
		for (Emprunt emprunt : empruntsProgrammes) {
			if (firstDate.after(emprunt.getDateDebut())) {
				firstDate = emprunt.getDateDebut();
			}
		}
		for (Emprunt emprunt : empruntsEnCours) {
			if (firstDate.after(emprunt.getDateDebut())) {
				firstDate = emprunt.getDateDebut();
			}
		}
		for (Emprunt emprunt : empruntsTermines) {
			if (firstDate.after(emprunt.getDateDebut())) {
				firstDate = emprunt.getDateDebut();
			}
		}
		for (Emprunt pret : pretsProgrammes) {
			if (firstDate.after(pret.getDateDebut())) {
				firstDate = pret.getDateDebut();
			}
		}
		for (Emprunt pret : pretsEnCours) {
			if (firstDate.after(pret.getDateDebut())) {
				firstDate = pret.getDateDebut();
			}
		}
		for (Emprunt pret : pretsTermines) {
			if (firstDate.after(pret.getDateDebut())) {
				firstDate = pret.getDateDebut();
			}
		}
		for (InvisibiliteProprietaire conge : conges) {
			if (firstDate.after(conge.getDateDebutInvisibiliteProp())) {
				firstDate = conge.getDateDebutInvisibiliteProp();
			}
		}
		//System.out.println("first date : " + firstDate);
		return firstDate;
	}

	public void setFirstDate(Date firstDate) {
		this.firstDate = firstDate;
	}

}
